﻿/*****************************************************************
// Copyright (C)
// All rights reserved.
// 
// Author:       WYQ
// Create Date:  2012-8-18 15:16:01
// Usage: 持久化数据库仓库
*****************************************************************/
using System;
using System.Linq;
using System.Linq.Expressions;

namespace Portal.Ef.Repository
{

   
    public class  MenuInRoleRepository : Repository<MenuInRole, MenuInRole, TbMenuInRole>, IMenuInRoleRepository<MenuInRole>
    {
        public override MenuInRole CreateEntity()
        {
            return new MenuInRole { };
        }
        public override IQueryable<MenuInRole> Get()
        {
            return MapQueryable(Entities.TbMenuInRole);
        }
        public override long Count()
        {
            return MapQueryable(Entities.TbMenuInRole).Count();
        }
        public override IQueryable<MenuInRole> Get(Expression<Func<MenuInRole, bool>> expression)
        {
            return MapQueryable(Entities.TbMenuInRole).Where(expression);
        }
        public override IQueryable<MenuInRole> GetSort(string sort = "Id", string order = "DESC")
        {
            Check.Argument.IsNotNull(sort, "排序字段");
            Check.Argument.IsNotNull(order, "排序条件");
            return LinqHelper.DataSorting(MapQueryable(Entities.TbMenuInRole), sort, order);
        }
        public override IQueryable<MenuInRole> GetSort(Expression<Func<MenuInRole, bool>> expression, string sort = "Id", string order = "DESC")
        {
            Check.Argument.IsNotNull(expression, "查询条件");
            Check.Argument.IsNotNull(sort, "排序字段");
            Check.Argument.IsNotNull(order, "排序条件");
            return LinqHelper.DataSorting(MapQueryable(Entities.TbMenuInRole).Where(expression), sort, order);
        }
        public override void Add(MenuInRole t)
        {
            Entities.AddToTbMenuInRole(ReverseMapModel(t));
        }
        public override void Update(MenuInRole t)
        {
            throw new NotImplementedException();
        }
        public override void AddOrUpdate(MenuInRole t)
        {
            if (Entities.TbMenuInRole.Any(o => o.CoMenuInRoleId == t.Id))
            {

            }
            else
            {
                Entities.AddToTbMenuInRole(ReverseMapModel(t));
            }
        }
        public override void Delete(MenuInRole t)
        {
            Entities.TbMenuInRole.Where(o => o.CoMenuInRoleId == t.Id).ForEach(o => Entities.DeleteObject(o));
        }
        public override void Delete(Expression<Func<MenuInRole, bool>> expression)
        {
            MapQueryable(Entities.TbMenuInRole).Where(expression).ForEach(o => Entities.DeleteObject(o));
        }
        public override void Save()
        {
            Entities.SaveChanges();
        }
       
        public JqEasyuiDataGrid<MenuInRole> JqEasyuiDataGrid(string code, string name, string categoryId, string sort = "CreateTime", string order = "Desc", int page = 10, int index = 1)
        {
            return new JqEasyuiDataGrid<MenuInRole>();
        }
      
        public override sealed IQueryable<MenuInRole> MapQueryable(IQueryable<TbMenuInRole> queryable)
        {
            return queryable.Select(o => new MenuInRole 
            {
         
	           Id=o.CoMenuInRoleId,
	           RoleId = o.CoRoleId 
,
	           MenuId = o.CoMenuId 
            
            });
        }
      
        public override sealed IQueryable<TbMenuInRole> ReverseMapQueryable(IQueryable<MenuInRole> queryable)
        {
            return queryable.Select(o => new TbMenuInRole 
            { 
        
	           CoMenuInRoleId=o.Id,
	           CoRoleId = o.RoleId
,
	           CoMenuId = o.MenuId
  
            
            });
        }
      
        public override sealed MenuInRole MapModel(TbMenuInRole model)
        {
            return new MenuInRole {
	           Id=model.CoMenuInRoleId,
	           RoleId = model.CoRoleId
,
	           MenuId = model.CoMenuId
            };
        }
        
        public override sealed TbMenuInRole ReverseMapModel(MenuInRole model)
        {
            return new TbMenuInRole { 
	           CoMenuInRoleId=model.Id,
	           CoRoleId = model.RoleId
	        
,
	           CoMenuId = model.MenuId
	        
            
            
            };
        }


    }
}



